package com.hhxy.login.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FTPUtil {

    /**
     * 打开FTP服务链接
     * @param ftpHost
     * @param ftpPort
     * @param ftpUserName
     * @param ftpPassword
     */
    public static FTPClient getFTPClient(String ftpHost, Integer ftpPort, String ftpUserName, String ftpPassword){
        FTPClient ftpClient = null;
        try {
            ftpClient = new FTPClient();
            ftpClient.setConnectTimeout(60000);
            if(ftpPort != null){
                ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            }else {
                ftpClient.connect(ftpHost);// 连接FTP服务器
            }
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                if (ftpClient.login(ftpUserName, ftpPassword)) {// 登陆FTP服务器
                    if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
                            "OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持，如果服务器支持就用UTF-8编码，否则就使用本地编码（GBK）.
                        ftpClient.setControlEncoding("UTF-8");
                    }else {
                        ftpClient.setControlEncoding("GBK");
                    }
                    ftpClient.enterLocalPassiveMode();// 设置被动模式
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// 设置传输的模式，以二进制流的方式读取
                    ftpClient.enterLocalPassiveMode();
                    System.out.println("FTP服务连接成功！");
                }else {
                    System.out.println("FTP服务用户名或密码错误！");
                    disConnection(ftpClient);
                }
            }else {
                System.out.println("连接到FTP服务失败！");
                disConnection(ftpClient);
            }
        } catch (SocketException e) {
            e.printStackTrace();
            disConnection(ftpClient);
            System.out.println("FTP的IP地址可能错误，请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            disConnection(ftpClient);
            System.out.println("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * 关闭FTP服务链接
     * @throws IOException
     */
    public static void disConnection(FTPClient ftpClient){
        try {
            if(ftpClient.isConnected()){
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件夹下的所有文件信息
     * @param path 文件路径
     */
    public static FTPFile[] getFTPDirectoryFiles(FTPClient ftpClient,String path){
        FTPFile[] files = null;
        try {
            ftpClient.changeWorkingDirectory(path);
            files = ftpClient.listFiles();
        }catch (Exception e){
            e.printStackTrace();
            //关闭连接
            disConnection(ftpClient);
            System.out.println("FTP读取数据异常！");
        }
        return files;
    }


    /**
     * 获取文件夹下的所有文件信息
     * @param path 文件路径
     */
    public static InputStream getFTPFile(FTPClient ftpClient,String path,String fileName){
        InputStream in = null;
        try {
            ftpClient.changeWorkingDirectory(path);
            FTPFile[] files = ftpClient.listFiles();
            if(files.length > 0){
                in  = ftpClient.retrieveFileStream(fileName);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("FTP读取数据异常！");
        }finally {
            //关闭连接
            disConnection(ftpClient);
        }
        return in;
    }

    /**
     * 下载获取ftp上修改时间、创建时间最新的文件
     * @param ftpClient
     * @return
     */
    public static String getNewestFileName(FTPClient ftpClient) {
        //获取ftp目录下所有文件
        FTPFile[] files= new FTPFile[0];
        try {
            files = ftpClient.listFiles();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //文件放入自定义集合
        List<FTPFile> list = new ArrayList<>(Arrays.asList(files));
        //根据文件修改时间获取最新的文件
        list.sort((file1, newFile) -> {
            if (file1.getTimestamp().compareTo(newFile.getTimestamp()) < 0) {
                return 1;
            } else if (file1.getTimestamp().compareTo(newFile.getTimestamp()) == 0) {
                return 0;
            } else {
                return -1;
            }
        });
        return list.get(0).getName();
    }

    public static void main(String args[]){
        InputStream in = null;
        BufferedReader br = null;
        try{
            String path = "/home/ftp/";
            //读取单个文件
            FTPClient ftpClient = getFTPClient("10.1.1.1",21,"username","123456");
            String fileName = "person.txt";
            in = getFTPFile(ftpClient,path,fileName);
            if(in != null){
                br = new BufferedReader(new InputStreamReader(in,"GBK"));
                String data = null;
                while ((data = br.readLine()) != null) {
                    String[] empData = data.split(";");
                    System.out.println(empData[0]+" "+empData[1]);
                }
            }

            //读取文件夹下的所有文件
            FTPClient ftpClient1 = getFTPClient("10.1.1.1",21,"username","123456");
            FTPFile[] files = getFTPDirectoryFiles(ftpClient1,path);
            if(files != null && files.length > 0){
                for (int i = 0; i < files.length; i++) {
                    if(files[i].isFile()){
                        in = ftpClient1.retrieveFileStream(files[i].getName());
                        br = new BufferedReader(new InputStreamReader(in,"GBK"));
                        System.out.println(files[i].getName());
                    }
                }
            }
            //关闭连接
            disConnection(ftpClient1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                //关闭流
                if (br != null)
                    br.close();
                if (in != null)
                    in.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}